/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Specialized;

using DotNetNuke.UI.WebControls;

#endregion

namespace DotNetNuke.ModuleToolkit.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An Edit Control to Edit files
    /// </summary>
    /// <history>
    /// 	[cnurse]	07/30/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class FileEditControl : IntegerEditControl
    {

        #region Private Fields

        private FileControl fileControl;
        private string _FileFilter;
        private string _FilePath; 

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the current file extension filter.
        /// </summary>
        /// <history>
        /// 	[anurse]	08/11/2006 documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public string FileFilter
        {
            get { return _FileFilter; }
            set { _FileFilter = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the current file path.
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/02/2007 created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string FilePath
        {
            get { return _FilePath; }
            set { _FilePath = value; }
        } 

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Creates the control contained within this control
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006 created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void CreateChildControls()
        {
            //First clear the controls collection
            Controls.Clear();

            //Create Table
            fileControl = new FileControl();
            fileControl.ID = this.ID + "file";
            fileControl.FileFilter = FileFilter;
            fileControl.FilePath = FilePath;

            //Add table to Control
            this.Controls.Add(fileControl);

            base.CreateChildControls();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Runs before the control is rendered.
        /// </summary>
        /// <history>
        /// 	[cnurse]	07/31/2006 created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            fileControl.FileID = IntegerValue;

            if (Page != null)
                this.Page.RegisterRequiresPostBack(this);

        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Renders the control in edit mode
        /// </summary>
        /// <param name="writer">An HtmlTextWriter to render the control to</param>
        /// <history>
        /// 	[cnurse]	04/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderEditMode(System.Web.UI.HtmlTextWriter writer)
        {
            this.RenderChildren(writer);
        } 

        #endregion

        #region Public Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Loads the Post Back Data and determines whether the value has change
        /// </summary>
        /// <remarks>
        /// In this case because the <see cref="FileControl"/> is a contained control, we do not need 
        /// to process the PostBackData (it has been handled by the File Control).  We just use
        /// this method as the Framework calls it for us.
        /// </remarks>
        /// <param name="postDataKey">A key to the PostBack Data to load</param>
        /// <param name="postCollection">A name value collection of postback data</param>
        /// <history>
        ///     [cnurse]	08/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            this.EnsureChildControls();
            bool dataChanged = false;
            if (IntegerValue != fileControl.FileID)
            {
                Value = fileControl.FileID;
                dataChanged = true;
            }
            return dataChanged;
        } 

        #endregion

    }
}
